class HomeController < ApplicationController
  
  def index
  end
  
  def search_document_info
    @search_by = {'ISBN' => 1, 'Document name' => 2, 'Author' => 3, 'Discription' => 4 }
  end
  
  def search_document
    if params[:search_by] and params[:key_word].length > 0
      key_word = params[:key_word]
      case params[:search_by][0]
        when "1"  then @document = Document.find(:all, :conditions => ["isbn LIKE ?", "%#{key_word}%"])
        when "2"  then @document = Document.find(:all, :conditions => ["name LIKE ?", "%#{key_word}%"])
        when "3"  then @document = Document.find(:all, :conditions => ["author LIKE ?", "%#{key_word}%"])
        when "4"  then @document = Document.find(:all, :conditions => ["description LIKE ?", "%#{key_word}%"])  
      end
      if @document
        respond_to do |format|
          format.html # list_borrowing.html.erb
          format.json { render json: @document }
        end
      else
        respond_to do |format|
            format.html { redirect_to search_document_info_url, notice: 'Can not find the document' }
        end
      end
    else
      respond_to do |format|
          format.html { redirect_to search_document_info_url, notice: 'Please fill the key word and choose the search method' }
      end
    end
  end
end
